package com.github.baichuan.application.scaffold.rest.controller;

import com.github.baichuan.application.scaffold.rest.domain.app.App;
import com.github.baichuan.application.scaffold.rest.domain.app.AppService;
import com.github.baichuan.application.scaffold.rest.dto.app.AppForm;
import com.github.baichuan.application.scaffold.rest.dto.app.AppTableView;
import com.github.baichuan.application.scaffold.rest.dto.app.AppQueryCriteria;
import com.github.baichuan.application.scaffold.rest.jpa.app.AppSpecification;
import com.github.baichuan.mapping.BeanMapper;
import com.github.baichuan.mapping.EnumDescriptionMapper;
import com.github.baichuan.web.dto.Pagination;
import com.github.baichuan.web.dto.PaginationCriteria;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.ui.select.OptionItem;
import com.github.baichuan.web.servlet.Restful;
import com.github.baichuan.web.ui.select.SelectUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "APP应用")
@RequestMapping("/app")
public class AppController {
    @Autowired
    private AppService appService;

    @Operation(summary = "查询")
    @PostMapping("/query")
    public ResponseEntity<Pagination<AppTableView>> query(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                                          @RequestBody PaginationCriteria<AppQueryCriteria> paginationCriteria){
        AppSpecification specification = paginationCriteria.getCriteria().specification(application);
        Page<App> page = appService.query(specification, paginationCriteria.pageable(AppTableView.class));
        Pagination<AppTableView> pagination = Pagination.of(page).map(AppTableView.class);
        return ResponseEntity.ok(pagination);
    }

    @Operation(summary = "获取表单数据")
    @GetMapping("/{id}")
    public ResponseEntity<AppForm> get(@PathVariable("id") String id) {
        App app = appService.find(id);
        AppForm form = BeanMapper.map(app, AppForm.class);
        return ResponseEntity.ok(form);
    }

    @Operation(summary = "添加")
    @PostMapping
    public ResponseEntity<Result> add(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                      @RequestBody @Validated AppForm form){
        Result result = appService.add(application, form);
        return Restful.created(result);
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    public ResponseEntity<Result> edit(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                        @PathVariable("id") String id,
                                        @RequestBody @Validated AppForm form){
        Result result = appService.edit(application, id, form);
        return Restful.response(result);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public ResponseEntity<Result> remove(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                         @PathVariable("id") String id){
        Result result = appService.remove(application, id);
        return Restful.response(result);
    }

    @Operation(summary = "平台选项")
    @GetMapping("/platform/options")
    public ResponseEntity<OptionItem[]> platform() throws IllegalAccessException {
        OptionItem[] options = SelectUtils.options(App.Platform.class);
        return ResponseEntity.ok(options);
    }
}
